﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Newtonsoft.Json.Linq;

namespace Kugar.Tools.CityTool.GenerateCityPickerData.Templates
{
    public partial class tshi0912_template
    {
        public tshi0912_template(JArray inputData)
        {
            CityData = decodeData(inputData);
        }

		public List<JObject> CityData { set; get; }

        public List<JObject> decodeData(JArray data)
        {
             var lst=new List<JObject>();

             foreach (JObject json in data)
             {
                 //foreach (var city in json.GetJObjectArray("sub"))
                 //{
                    getFlatData(json, lst);
                 //}
                 
             }

             return lst;
        }

        private void getFlatData(JObject parent, List<JObject> result)
        {

            //foreach (JObject json in parent.GetJArray("sub"))
            //{
                result.Add(new JObject()
                {
                    ["code"] = parent.GetString("code"),
                    ["name"] = parent.GetString("name"),
                    ["sub"] = new JArray(parent.GetJObjectArray("sub").Select(x=>new JObject()

                    {
                        ["code"]=x.GetString("code"),
                        ["name"]=x.GetString("name")
                    }))
                });

                var sub = parent.GetJObjectArray("sub");

                foreach (JObject j in sub)
                {
                    if (j.ContainsKey("sub"))
                    {
                        getFlatData(j,result);
                    }
                }
            //}
        }
    }
}
